<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://www.springframework.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>权限设置</title>
    <link rel="stylesheet" href="../../lib/layui/layui/css/layui.css">
    <script type="text/javascript" src="../../js/news/jquery-1.9.1.js"></script>
    <script src="../../js/base/base.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="../../lib/layui/layui/lay/dest/layui.all.js"></script>
    <script src="../../lib/layui/layui/layui.js"></script>

    <style>
        * {font-family: "Microsoft Yahei" !important;}
        nav button{
            margin-left: 5px;
            padding: .25rem .5rem;
            font-size: .875rem;
            border-radius: .2rem;
            margin-top: 10px;
            margin-right: 22px;
        }
        nav div{
            float: left !important;
            margin: 15px;
        }
        nav{
            height: 50px;
            border-bottom: 1px solid #cfdbe2;
            background-color: #fafbfc;
            border-radius: 0;
        }
        .layui-tab{
             margin:0;
        }
        .content{
            height: 544px;
            overflow: auto;
            margin: 14px;
            padding: 14px;
            background: #fff;
        }
        .layui-tab-content{
            background-color: #F5F7FA;
        }
        .search{
            display: inline;
            margin-bottom: 1px;
            width: 185px;
            border: 0;
            background: #fafbfc;
        }
        .one{
            border: 1px solid #03a9f4;
            border-radius: 5px;
            color: #03a9f4;
            border-color: #03a9f4;
            background: #fafbfc;
        }
        .layui-table-tool-self {
            display: none;
        }
    </style>
</head>
<body>
<form class="site-content layui-form">
    <nav>
        <div>权限设置</div>
        <button type="button" class="layui-btn layui-btn-normal layui-btn-sm addBtn" style="float: right !important;">新建</button>
        <%--<div style="display: inline-block;border-bottom: 1px solid #ccc;margin-top: 4px;">--%>
            <%--<input type="text" class="search" placeholder="搜索"/>--%>
            <%--<span style="padding: 5px;color: #999;cursor: pointer;">--%>
                <%--<i class="layui-icon layui-icon-search"></i>--%>
            <%--</span>--%>
        <%--</div>--%>
    </nav>
    <div>
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <div class="layui-tab-content">
                <div class="content home"><table class="layui-hide" id="plan" lay-filter="tableInt"></table></div>
            </div>
        </div>
    </div>
    <div style="display: none" id="newContent">
        <div class="layui-form-item" style="margin-top: 15px">
            <label class="layui-form-label">管理者：<span style="color:red">*</span></label>
            <div class="layui-input-block">
                <textarea  name="userIdAdmin" id="userIdAdmin" style="min-height: 70px;width: 270px;border-radius: 3%" user_id=" "   lay-verify="required" placeholder="请选择管理人员" class=" textarea exchange_user" disabled></textarea>
                <a href="javascript:;" id="userIdAdminAdd"  style="color: blue;">添加</a>
                <a href="javascript:;" id="userIdAdminDel"  style="color: red;">清空</a>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限设置：<span style="color:red">*</span></label>
            <div class="layui-input-block" style="width: 270px" lay-filter="test1">
                <select id="selects" name="selects" autocomplete="off" lay-verify="required" lay-filter="searchSelects" >
                    <option value="5" >选择管理范围</option>
                    <option value="2" >指定部门</option>
                    <option value="3" >指定角色</option>
                    <option value="4" >指定人员</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item" style="display: none" id="department">
            <label class="layui-form-label">指定部门：<span style="color:red">*</span></label>
            <div class="layui-input-block">
                <textarea  name="deptIds" id="deptId" style="min-height: 70px;width: 270px;border-radius: 3%" user_id=" "   lay-verify="required" placeholder="请选择部门范围" class=" textarea exchange_user" disabled></textarea>
                <a href="javascript:;" id="deptIdAdd"  style="color: blue;">添加</a>
                <a href="javascript:;" id="deptIdDel"  style="color: red;">清空</a>
            </div>
            <div class="layui-form-item" style="margin-top: 15px">
                <label class="layui-form-label" style="width: 90px">包含子部门：</label>
                <div class="layui-input-block" >
                    <input  id="smsRemind" type="checkbox"  name="smstype" value=""   lay-skin="primary" >

                </div>
            </div>
        </div>
        <div class="layui-form-item" style="display: none" id="role">
            <label class="layui-form-label">指定角色：<span style="color:red">*</span></label>
            <div class="layui-input-block">
                <textarea  name="privIds" disabled id="privId" style="min-height: 70px;width: 270px;border-radius: 3%" user_id=" "   lay-verify="required" placeholder="请选择角色范围" class=" textarea exchange_user" ></textarea>
                <a href="javascript:;" id="privIdAdd"  style="color: blue;">添加</a>
                <a href="javascript:;" id="privIdDel"  style="color: red;">清空</a>
            </div>

        </div>
        <div class="layui-form-item" style="display: none" id="peopple">
            <label class="layui-form-label">指定人员：<span style="color:red">*</span></label>
            <div class="layui-input-block">
                <textarea  name="userIds" disabled id="userId" style="min-height: 70px;width: 270px;border-radius: 3%" user_id=" "   lay-verify="required" placeholder="请选择人员范围" class=" textarea exchange_user" ></textarea>
                <a href="javascript:;" id="userIdAdd"  style="color: blue;">添加</a>
                <a href="javascript:;" id="userIdDel"  style="color: red;">清空</a>
            </div>
        </div>
        <blockquote class="layui-elem-quote layui-quote-nm"><span style="font-weight:bold;">权限应用于:</span> <span style="margin-left: 10px">费用统计</span></blockquote>
    </div>
</form>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">批量删除</button>
    </div>
</script>
</body>
<script>
    $('.content').height($(document.body).height()-50);
    //管理人员的添加
    $("#userIdAdminAdd").on("click",function(){
        user_id = 'userIdAdmin';
        $.popWindow("/common/selectUser");
    });
    //管理人员的删除
    $('#userIdAdminDel').click(function () {
        $('#userIdAdmin').attr("dataid","");
        $('#userIdAdmin').attr("user_id","");
        $('#userIdAdmin').attr("username","");
        $('#userIdAdmin').attr("userprivname","");
        $('#userIdAdmin').val("");
    });
    //部门的添加
    $("#deptIdAdd").on("click",function(){
        dept_id = 'deptId';
        $.popWindow("/common/selectDept");
    });
    //部门的删除
    $('#deptIdDel').click(function () {
        $('#deptId').attr("deptid","");
        $('#deptId').attr("deptno","");
        $('#deptId').attr("deptname","");
        $('#deptId').val("");
    });
    //角色的添加
    $("#privIdAdd").on("click",function(){
        priv_id = 'privId';
        $.popWindow("/common/selectPriv");
    });
    //角色的删除
    $('#privIdDel').click(function () {
        $('#privId').attr("privid","");
        $('#privId').attr("userpriv","");
        $('#privId').val("");
    });
    //人员的添加
    $("#userIdAdd").on("click",function(){
        user_id = 'userId';
        $.popWindow("/common/selectUser");
    });
    //人员的删除
    $('#userIdDel').click(function () {
        $('#userId').attr("dataid","");
        $('#userId').attr("user_id","");
        $('#userId').attr("username","");
        $('#userId').attr("userprivname","");
        $('#userId').val("");
    });
    layui.use(['form', 'layedit', 'laydate','layedit','table','layer'], function(){
        var form = layui.form
            ,table = layui.table
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate

        form.render();

        var tableInt =  table.render({
            elem: '#plan'
           ,url:'/chargePermission/findPermissionAll'
            ,page: true //开启分页
            ,toolbar: '#toolbarDemo'
            ,limit: 10
            ,cellMinWidth: 80
            ,cols: [[
                {type:'checkbox'}
                ,{field:'managerUsersName', title: '管理者'}
                ,{field:'setType', title: '管理范围', templet: function(d) {
                        if(d.setType==2){ return '指定部门'}
                        if(d.setType==3){ return '指定角色'}
                        if(d.setType==4){ return '指定人员'}
                    }}
                ,{title:'操作', toolbar: '#barDemo'}
            ]]
            ,response: {
                statusName: 'flag',  //规定数据状态的字段名称，默认：code
                statusCode: true,   //规定成功的状态码，默认：0
                dataName: 'obj',    //规定数据列表的字段名称，默认：data
                countName: 'totleNum'
            }
        });
        //行具栏事件
        table.on('tool(tableInt)', function(obj){
            var data = obj.data;
            var chargePermissionId=data.chargePermissionId
            if(obj.event === 'del'){
                layer.confirm('确定删除当前数据吗？',function (index) {
                    $.ajax({
                        type: 'get',
                        url: '/chargePermission/delPermissionIds',
                        dataType: 'json',
                        data:{
                            chargePermissionIds:chargePermissionId
                        },
                        success: function (res) {
                            if (res.msg == 'ok') {
                                layer.msg('删除成功！', {icon: 1});
                                tableInt.reload()
                                layer.closeAll();
                            }else{    layer.msg('删除失败！', {icon: 0});

                            }
                        }
                    })
                });
            } else if(obj.event === 'edit'){
                create(1,chargePermissionId)
            }

        });

        //头工具栏事件
        table.on('toolbar(tableInt)', function(obj){
            var checkStatus = table.checkStatus('plan'); //获取选中行状态
            var data = checkStatus.data;  //获取选中行数据
            if (data.length==0){
                layer.msg('最少选中一项!');
                return false;
            };
            switch(obj.event){
                case 'getCheckData':
                    var ids ='';
                    for(var i in data){
                        ids+=data[i].chargePermissionId+',';
                    }
                    $.get('/chargePermission/delPermissionIds',{chargePermissionIds:ids},function (res) {
                        if (res.flag){
                            layer.msg('删除成功！');
                            tableInt.reload()
                        }
                    });
                    break;
            };
        });
        $('.addBtn').click(function () {
            create(0,'')
        })
        function create(type,chargePermissionId){
            resetForm(form)
            if(type=='0'){
                $("#selects option:first").prop("selected", 'selected');
                form.render()
                var title = '新建'
            }else{
                var title = '编辑'
            }
            layer.open({
                type: 1,
                title: title,
                btn:['确定','取消'],
                shade: 0.5,
                area: ['540px', '430px'],
                content: $('#newContent'),
                success:function(res){

                    //编辑回显
                    if(type==1){
                        $.ajax({
                            url:'/chargePermission/findPermissionById',
                            type: 'get',
                            dataType: 'json',
                            data:{chargePermissionId:chargePermissionId},
                            success:function (res) {
                                $("#selects option[value='"+res.object.setType+"']").prop("selected", true);
                                if(res.object.hasChildren == 1){
                                    $("#smsRemind").prop("checked",true);
                                }
                                form.render();
                                $('#userIdAdmin').val(res.object.managerUsersName)
                                $("#userIdAdmin").attr('user_id',res.object.managerUsers);
                                if(res.object.setType=="2"){
                                    $('#department').show()
                                    $('#role').hide()
                                    $('#peopple').hide()
                                    $('#deptId').val(res.object.managerAreaName)
                                    $("#deptId").attr('deptid',res.object.managerArea);
                                }
                                if(res.object.setType=="3"){
                                    $('#role').show()
                                    $('#peopple').hide()
                                    $('#department').hide()
                                    $('#privId').val(res.object.managerAreaName)
                                    $("#privId").attr('privid',res.object.managerArea);
                                }
                                if(res.object.setType=="4"){
                                    $('#peopple').show()
                                    $('#department').hide()
                                    $('#role').hide()
                                    $('#userId').val(res.object.managerAreaName)
                                    $("#userId").attr('user_id',res.object.managerArea);
                                }
                                // resetForm(form)
                            }
                        })
                    }
                    form.on('select(searchSelects)', function(data){
                    var datas = data.value
                      if(datas == 2){
                          $('#department').show()
                          $('#role').hide()
                          $('#peopple').hide()
                      }
                        if(datas == 3){
                            $('#role').show()
                            $('#peopple').hide()
                            $('#department').hide()
                        }
                        if(datas == 4){
                            $('#peopple').show()
                            $('#department').hide()
                            $('#role').hide()
                        }
                        if(datas == 5){
                            $('#department').hide()
                            $('#role').hide()
                            $('#peopple').hide()
                        }
                    });
                },
                yes:function(index){
                    var managerUsersName = $('textarea[name="userIdAdmin"]').val()
                    var str = ''
                    var strs = $('select[name="selects"]').val()
                    var str1 = ''
                    var datad = ''
                    var hasChildren= 2
                    if($('#smsRemind').is(':checked')){
                        hasChildren=1;
                    }
                    if(strs == 2){
                        str = '2'
                        datad = $('textarea[name="deptIds"]').attr('deptid')
                        str1 = $('textarea[name="deptIds"]').val()
                    }
                    if(strs == 3){
                        str = '3'
                        datad = $('textarea[name="privIds"]').attr('privid')
                        str1 = $('textarea[name="privIds"]').val()
                    }
                    if(strs == 4){
                        str = '4'
                        datad = $('textarea[name="userIds"]').attr('user_id')
                        str1 = $('textarea[name="userIds"]').val()
                    }

                    if(managerUsersName == '' || strs== 5 || datad == ''){alert('带星号为必选项！')}


                    var data = {
                        managerUsers : $('textarea[name="userIdAdmin"]').attr('user_id'),
                        managerUsersName : managerUsersName,
                        managerArea : datad,
                        managerAreaName:str1,
                        setType:str,
                        managerType:3,
                        hasChildren:hasChildren
                    }
                    if(type == 0){
                        var url = '/chargePermission/enidOrInsertPermission'

                    }else{
                        var url = '/chargePermission/enidOrInsertPermission'
                        data.chargePermissionId = chargePermissionId;
                    }

                    if(managerUsersName != '' && strs!= 5 && datad != ''){
                        $.ajax({
                            url:url,
                            data:data,
                            dataType:'json',
                            type:'post',
                            success:function (res) {
                                layer.msg('新增成功!')
                                tableInt.reload()
                                layer.closeAll();
                                resetForm(form)
                            }
                        })
                        resetForm(form)
                    }


                }
            })
        }

    });
    function resetForm(form) {
        $('#userIdAdmin').attr("dataid","");
        $('#userIdAdmin').attr("user_id","");
        $('#userIdAdmin').attr("username","");
        $('#userIdAdmin').attr("userprivname","");
        $('#userIdAdmin').val("");
        $('#deptId').attr("deptid","");
        $('#deptId').attr("deptno","");
        $('#deptId').attr("deptname","");
        $('#deptId').val("");
        $('#privId').attr("privid","");
        $('#privId').attr("userpriv","");
        $('#privId').val("");
        $('#userId').attr("dataid","");
        $('#userId').attr("user_id","");
        $('#userId').attr("username","");
        $('#userId').attr("userprivname","");
        $('#userId').val("");
        $('#peopple').hide()
        $('#department').hide()
        $('#role').hide()
        $("#smsRemind").prop("checked",false);
    }

</script>
</html>
